//
//  TabBarViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/6.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "TabBarViewController.h"
#import "BaseNavigationController.h"
#import "OMPersonalCenterViewController.h"

#import "OMShoppingCartViewController.h"

#import "OMPersonalCenterOrderViewController.h"
#import "OMConsumerGoodsViewController.h"
#import "OMHomeViewController.h"
@interface TabBarViewController ()

@end

@implementation TabBarViewController{
    NSInteger _index;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self addChildVc:[[OMHomeViewController alloc] init]
                   title:@"首页"
                   image:@"tab_home"
           selectedImage:@"tab_home_select"];
        [self addChildVc:[[OMConsumerGoodsViewController alloc] init]
                   title:@"分类"
                   image:@"tab_category_unselect_25x25_"
           selectedImage:@"tab_category_select_25x25_"];
        [self addChildVc:[[OMPersonalCenterOrderViewController alloc] init]
                   title:@"订单"
                   image:@"tab_order_unselect_25x25_"
           selectedImage:@"tab_order_select_25x25_"];
        [self addChildVc:[[OMPersonalCenterViewController alloc] init]
                   title:@"个人中心"
                   image:@"tab_personal"
           selectedImage:@"tab_personal_select"];

    }
    return self;
}


UIImageView *shadowIV;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self.tabBar respondsToSelector:@selector(setTranslucent:)]) {
        self.tabBar.translucent = NO;
    }

    //改变tabbar 线条颜色
//    [self.tabBar setShadowImage:[UIImage new]];
//    shadowIV = [[UIImageView alloc]initWithImage:[UIImage pngNamed:@"ic_tabbar_shadow"]];
//    [shadowIV sizeToFit];
//    [self.tabBar addSubview:shadowIV];
    [self.tabBar setBackgroundImage:[UIImage imageWithColor:SM_whiteColor]];


    //通过这两个参数来调整badge位置
    [self.tabBar setTabIconWidth:45];
    [self.tabBar setBadgeTop:9];
    self.tabBar.tintColor = SM_thumeColor;

    //加载购物车的数量

}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    CGRect frame = shadowIV.frame;
    frame.origin.x = 0;
    frame.origin.y = -4;
    frame.size.width = self.view.frame.size.width;
    shadowIV.frame = frame;
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (UIViewController *)addChildVc:(BaseViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;

    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


    //customFont
    UIFont *customFont = SM_CFont(20);

    // 设置文字的样式
    [childVc.tabBarItem setTitleTextAttributes:@{
                                                 NSForegroundColorAttributeName : SM_grayColor,
                                                 NSFontAttributeName            : customFont
                                                 }
                                      forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{
                                                 NSForegroundColorAttributeName : SM_blackColor,
                                                 NSFontAttributeName            : customFont
                                                 }
                                      forState:UIControlStateSelected];

    UINavigationController *navigationController = navigationCtrlMake(childVc);
    // 添加子控制器
    [self addChildViewController:navigationController];
    return childVc;
}



- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectIndex = [tabBar.items indexOfObject:item];
    if (selectIndex != _index) [self animationWithIndex:selectIndex];
}

- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews)
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")])
            [tabbarbuttonArray addObject:tabBarButton];
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount = 1;
    pulse.autoreverses = YES;
    pulse.fromValue = [NSNumber numberWithFloat:0.7];
    pulse.toValue = [NSNumber numberWithFloat:1.2];
    [[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];

    _index = index;
}

@end
